package com.marvels.financial.pay.ybpay;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.marvels.common.utils.MarvelsLogUtil;
import com.yeepay.g3.sdk.yop.client.YopClient3;
import com.yeepay.g3.sdk.yop.client.YopRequest;
import com.yeepay.g3.sdk.yop.client.YopResponse;

/**
 * 
* @ClassName: Api 
* @Description: 易宝对接通信工具类 
* @author 李瑶
* @date 2018年10月6日 下午3:36:05 
*
 */
public class Api {
	
	private static  MarvelsLogUtil logger = MarvelsLogUtil.getInstance();
	
	/**
	 * 
	* @Title: ybPost 
	* @Description: 往易宝发送请求 
	* @param map 请求参数集合
	* @param Uri 请求的子路由
	* @return
	* @throws IOException       
	* @author 李瑶
	* @date 2018年10月6日 下午4:01:26
	 */
	 public static Map<String,Object> ybPost(Map<String,String> map,String uri,String appKey){
		Map<String,Object> result  =  new HashMap<String,Object>();
		try {
			logger.info("Api ybPost system preperty====>"+System.getProperty("yop.sdk.config.dir"));
			logger.info("Api ybPost system preperty====>"+System.getProperty("yop.sdk.config.file"));
			YopRequest yoprequest  =  new YopRequest(appKey);
	        
	        
	        Set<Entry<String,String> > entry		=	map.entrySet();
	    	for(Entry<String,String> s:entry){
	    		yoprequest.addParam(s.getKey(), s.getValue());
	    	}
	    	logger.info("yoprequest:"+yoprequest.getParams());
	    	System.out.println("yoprequest:"+yoprequest.getParams());
	
	    	//向YOP发请求
	    	//uri = serverRoot + uri;
	    	YopResponse yopresponse = YopClient3.postRsa(uri, yoprequest);
			
	    	logger.info("请求YOP之后结果："+yopresponse.getStringResult());
	    	System.out.println("请求YOP之后结果："+yopresponse.getStringResult());
	    	
		    //对结果进行处理
	    	String state = yopresponse.getState();
	    	if("FAILURE".equals(state)){
				if(yopresponse.getError() != null){
					result.put("errorcode",yopresponse.getError().getCode());
					result.put("errormsg",yopresponse.getError().getMessage());
					logger.info("错误明细："+yopresponse.getError().getSubErrors());
					logger.info("系统处理异常结果："+result);
					return result;
				}else{
					result.put("errorcode","-1");
					result.put("errormsg","请求易宝接口系统级别异常");
					logger.info("错误明细：系统级别异常，异常状态state:"+state);
					logger.info("系统处理异常结果："+result);
					return result;
				}
				
			}
			//成功则进行相关处理
			if (yopresponse.getStringResult() != null) {
				result = parseResponse(yopresponse.getStringResult());
				logger.info("yopresponse.getStringResult: "+result);
	
	   		}
		} catch (IOException e) {
			logger.error("调用易宝的包，爆出异常", e);
			result.put("errorcode","-1");
			result.put("errormsg","请求易宝接口系统级别异常");
		}
  		return result;
     }
        
        

	//将获取到的yopresponse转换成json格式
	public static Map<String, Object> parseResponse(String yopresponse){
		
		Map<String,Object> jsonMap  = new HashMap<>();
		jsonMap	= JSON.parseObject(yopresponse, new TypeReference<TreeMap<String,Object>>() {});
		logger.info("将结果yopresponse转化为map格式之后: "+jsonMap);
		return jsonMap;
	}
    
	
	public static String getDateStr(){
		Date date = new Date();
		SimpleDateFormat sb = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sb.format(date);
	}
	
        
}
